/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.manager;

import br.com.deployment.entidade.registroimovel.AreaInterpi;
import br.com.deployment.entidade.registroimovel.GeoRef;
import br.com.deployment.servico.AreaInterpiServico;
import br.com.deployment.servico.MapServico;
import br.com.deployment.util.CoordinateConversion;
import br.com.deployment.utilitario.Msg;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import org.primefaces.event.map.StateChangeEvent;
import org.primefaces.model.DefaultUploadedFile;
import org.primefaces.model.UploadedFile;
import org.primefaces.model.map.*;

/**
 *
 * @author roniere
 */
@ManagedBean
@ViewScoped
public class managerAreaInterpi implements Serializable {

    @EJB
    AreaInterpiServico areaInterpiServico;
    @EJB
    MapServico mapServico;
    private String convN;
    private String convE;
    private String convZona;
    private String zoomMap;
    private String centerMap;
    private List<GeoRef> lstGeoRef;
    private MapModel polygonModel;
    private AreaInterpi areaInterpi;
    private UploadedFile fileCoord;

    @PostConstruct
    public void init() {
        this.areaInterpi = new AreaInterpi();
        this.lstGeoRef = new ArrayList<GeoRef>();
        this.polygonModel = new DefaultMapModel();
        this.centerMap = "-2.9136, -41.7577";
        this.zoomMap = "10";
        fileCoord = new DefaultUploadedFile();
        
          HttpServletRequest request = (HttpServletRequest) FacesContext
        .getCurrentInstance().getExternalContext().getRequest(); 
        
        if (request.getParameter("id") != null) {
            importEsc(request.getParameter("id"));
        }
        
    }

    public void importEsc(String n) {
       try {
            InputStreamReader isr = new InputStreamReader(new FileInputStream("/opt/INTERPI/"+n+".txt"));
            BufferedReader in = new BufferedReader(isr);
            String s;
            CoordinateConversion cc = new CoordinateConversion();
            while ((s = in.readLine()) != null) {
                s.replace("", "").replace(" ", "").replace("\n", "").replace("\\s", "");
                String lat = s.split(",")[1];
                String lon = "23" + s.split(",")[0];
                String zona = "M";

                String UTM = lon.toString().substring(0, 2) + " " + zona + " " + lon.toString().substring(2) + " " + lat.toString();
                lstGeoRef.add(new GeoRef(cc.utm2LatLon(UTM)[0], cc.utm2LatLon(UTM)[1], lon, lat, zona));
                initMap();
            }
            in.close();
        } catch (IOException ex) {
            Logger.getLogger(managerAreaInterpi.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void importFile(){

        try {
            File diretorio = new File("/opt/INTERPI/");
            File[] arquivos = diretorio.listFiles();
            for (File file : arquivos) {
                if (!file.getName().contains("~")) {
                    System.err.println("-----" + file.getName());
                    InputStreamReader isr = new InputStreamReader(new FileInputStream(file));
                    BufferedReader in = new BufferedReader(isr);
                    String s;
                    CoordinateConversion cc = new CoordinateConversion();
                    while ((s = in.readLine()) != null) {
                        s.replace("", "").replace(" ", "").replace("\n", "").replace("\\s", "");
                        String lat = s.split(",")[1];
                        String lon = "23" + s.split(",")[0];
                        String zona = "M";

                        String UTM = lon.toString().substring(0, 2) + " " + zona + " " + lon.toString().substring(2) + " " + lat.toString();
                        lstGeoRef.add(new GeoRef(cc.utm2LatLon(UTM)[0], cc.utm2LatLon(UTM)[1], lon, lat, zona));
                        areaInterpi.setDescricao("" + file.getName());
                        initMap();
                    }
                    in.close();
                    salvarCoord();
                    Thread.sleep(3000);
                    areaInterpi = new AreaInterpi();
                }
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(managerAreaInterpi.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(managerAreaInterpi.class.getName()).log(Level.SEVERE, null, ex);
        }
}

    public void gerarCoord() throws IOException {
        System.err.println(fileCoord.getFileName());
        InputStreamReader isr = new InputStreamReader(fileCoord.getInputstream());
        BufferedReader in = new BufferedReader(isr);
        String s;
        while ((s = in.readLine()) != null) {
            System.err.println(s);
        }
        in.close();
        br.com.deployment.message.Msg.messagemInfo("Ato Concluído com Sucesso !");
    }

    public void converterCoord() {
        String lat = convN;
        String lon = convE;
        String zona = convZona;

        try {
            lat = lat.replace(" ", "");
            lon = lon.replace(" ", "");
            if (zona == null) {
                zona = "M";
            }

            CoordinateConversion cc = new CoordinateConversion();

            String UTM = lon.toString().substring(0, 2) + " " + zona + " " + lon.toString().substring(2) + " " + lat.toString();
            lstGeoRef.add(new GeoRef(cc.utm2LatLon(UTM)[0], cc.utm2LatLon(UTM)[1], lon, lat, zona));

        } catch (Exception e) {
            Msg.messagemError("Valores incorretos");
        }


    }

    public void initMap() {
        if (lstGeoRef.size() >= 1) {
            polygonModel = new DefaultMapModel();
            Polygon polygon = new Polygon();

            Double centerX = 0D;
            Double centerY = 0D;
            int tam = lstGeoRef.size();
            for (GeoRef geoRef : lstGeoRef) {
                if (geoRef.getLat() != 0.0 && geoRef.getLng() != 0.0) {
                    polygon.getPaths().add(new LatLng(geoRef.getLat(), geoRef.getLng()));
                    polygonModel.addOverlay(new Marker(new LatLng(geoRef.getLat(), geoRef.getLng())));
                    centerX += geoRef.getLat() / tam;
                    centerY += geoRef.getLng() / tam;
                    if (polygon.getPaths().size() == 3) {
                        calcArea(polygon);
                    }
                }
            }

            if (tam >= 4 && tam < 6) {
                zoomMap = "18";
            } else if (tam >= 6) {
                zoomMap = "14";
            } else if (tam >= 8) {
                zoomMap = "12";
            }
            centerMap = lstGeoRef.get(0).getLat() + ", " + lstGeoRef.get(0).getLng();

            polygon.setStrokeColor("#FF9900");
            polygon.setFillColor("#FF9900");
            polygon.setStrokeOpacity(0.6);
            polygon.setFillOpacity(0.5);

            polygonModel.addOverlay(polygon);

            for (Marker marker : polygonModel.getMarkers()) {
                marker.setDraggable(true);
            }
        } else {
            polygonModel = new DefaultMapModel();
            this.centerMap = "-2.9136, -41.7577";
            this.zoomMap = "23";
        }
    }

    public double calcArea(Polygon pl) {

        WKTReader reader = new WKTReader(new GeometryFactory());

        String p = "POLYGON(( ";
        for (LatLng ll : pl.getPaths()) {
            p += ll.getLat() + " " + ll.getLng() + ", ";
        }
        p += "" + pl.getPaths().get(0).getLat() + " " + pl.getPaths().get(0).getLng() + " )) ";

        com.vividsolutions.jts.geom.Polygon polygonInt = null;
        try {
            polygonInt = (com.vividsolutions.jts.geom.Polygon) reader.read(p);
            System.err.println(polygonInt.getArea() + ": Area");
            System.err.println(polygonInt.getLength() + "LENGTH");
            System.err.println(polygonInt.getBoundary());
            System.err.println(polygonInt.getDimension());
        } catch (ParseException ex) {
            Logger.getLogger(ManagerCoord.class.getName()).log(Level.SEVERE, null, ex);
        }
        return polygonInt.getArea();

    }

    public void salvarCoord() {
        areaInterpi.setAtivo(true);
        areaInterpi.setGeoRef(lstGeoRef);
        areaInterpiServico.save(areaInterpi);
        Msg.messagemInfo("Área Interpi salva com sucesso");
    }

    public void onStateChange(StateChangeEvent event) {
        int zoomLevel = event.getZoomLevel();
        zoomMap = String.valueOf(zoomLevel);
    }

    public void removeGeoRef(GeoRef gf) {
        this.lstGeoRef.remove(gf);
        initMap();
    }

    public UploadedFile getFileCoord() {
        return fileCoord;
    }

    public void setFileCoord(UploadedFile fileCoord) {
        this.fileCoord = fileCoord;
    }

    public String getCenterMap() {
        return centerMap;
    }

    public void setCenterMap(String centerMap) {
        this.centerMap = centerMap;
    }

    public String getConvE() {
        return convE;
    }

    public void setConvE(String convE) {
        this.convE = convE;
    }

    public String getConvN() {
        return convN;
    }

    public void setConvN(String convN) {
        this.convN = convN;
    }

    public String getConvZona() {
        return convZona;
    }

    public void setConvZona(String convZona) {
        this.convZona = convZona;
    }

    public List<GeoRef> getLstGeoRef() {
        return lstGeoRef;
    }

    public void setLstGeoRef(List<GeoRef> lstGeoRef) {
        this.lstGeoRef = lstGeoRef;
    }

    public MapModel getPolygonModel() {
        return polygonModel;
    }

    public void setPolygonModel(MapModel polygonModel) {
        this.polygonModel = polygonModel;
    }

    public String getZoomMap() {
        return zoomMap;
    }

    public void setZoomMap(String zoomMap) {
        this.zoomMap = zoomMap;
    }

    public AreaInterpi getAreaInterpi() {
        return areaInterpi;
    }

    public void setAreaInterpi(AreaInterpi areaInterpi) {
        this.areaInterpi = areaInterpi;
    }
}
